

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:mobile_vpn/common/cpn/cpn_view_state.dart';

import 'package:mobile_vpn/pages/discovery_page/discovery_logic.dart';
import 'package:mobile_vpn/theme/color_palettes.dart';

import '../../http/api/vpn_plugin.dart';
import '../../router/routers.dart';

class DiscoveryPage extends CpnViewState<DiscoveryLogic> {

  DiscoveryPage({Key? key}) : super(key: key, bindViewState: false);

  static const platform = MethodChannel('com.example.vpn');

  RxBool isConnected = false.obs;
  final List<DiscoveryItem> discoveryItems = [
    DiscoveryItem(
      icon: Icons.public,
      title: "跨境留学",
      unreadCount: 5,
      onTap: (context) {
        AppRoutes.jumpPage(AppRoutes.loginPage);
      },
    ),
    DiscoveryItem(
      icon: Icons.article,
      title: "跨境头条",
      unreadCount: 0,
      onTap: (context) {},
    ),
    DiscoveryItem(
      icon: Icons.shopping_cart,
      title: "商城",
      unreadCount: 12,
      onTap: (context) {},
    ),
  ];

  @override
  Widget buildBody(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorPalettes.instance.background,
      appBar: AppBar(
        title: const Text('发现'),
        centerTitle: true,
        backgroundColor: Colors.transparent,
        titleTextStyle: TextStyle(
          color: ColorPalettes.instance.firstText,
          fontSize: 36.sp,
          fontWeight: FontWeight.w500,
        ),
      ),
      body: ListView.separated(
        itemCount: discoveryItems.length,
        separatorBuilder: (context, index) => Divider(
          height: 1.h,
          indent: 60.w,
          color: Colors.grey[200],
        ),
        itemBuilder: (context, index) {
          final item = discoveryItems[index];
          return DiscoveryListItem(
            item: item,
            onTap: () async {
              if (index == 1) { // 假设跨境头条是第二个元素
                await NativeVpnService.createProfile();


              }
            },
          );
        },
      ),
    );
  }


  void showSuccessDialog(BuildContext context, String message) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('成功'),
          content: Text(message),
          actions: <Widget>[
            TextButton(
              child: Text('确定'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  void showErrorDialog(BuildContext context, String message) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('错误'),
          content: Text(message),
          actions: <Widget>[
            TextButton(
              child: Text('确定'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
}

class DiscoveryListItem extends StatelessWidget {
  final DiscoveryItem item;
  final VoidCallback onTap;

  const DiscoveryListItem({
    super.key,
    required this.item,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Container(
        width: 36.w,
        height: 36.h,
        decoration: BoxDecoration(
          color: Colors.blue.withOpacity(0.1),
          borderRadius: BorderRadius.circular(6.r),
        ),
        child: Icon(item.icon, color: Colors.blue, size: 26.sp),
      ),
      title: Text(
        item.title,
        style: TextStyle(fontSize: 32.sp),
      ),
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (item.unreadCount > 0)
            Container(
              width: 36.w,
              height: 36.w,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(18.w),
              ),
              child: Text(
                item.unreadCount.toString(),
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 24.sp,
                ),
              ),
            ),
          SizedBox(width: 8.w),
          Icon( Icons.arrow_forward_ios, color: ColorPalettes.instance.firstIcon, size: 28.sp),
        ],
      ),
      contentPadding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 10.h),
      onTap: onTap,
    );
  }
}

class DiscoveryItem {
  final IconData icon;
  final String title;
  final int unreadCount;
  final Function(BuildContext) onTap; // 修改为接受BuildContext参数

  DiscoveryItem({
    required this.icon,
    required this.title,
    required this.unreadCount,
    required this.onTap,
  });
}
